package edu.sjtu.erplab.cm.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;


import com.cwec.fault.GenericFault;

import edu.sjtu.erplab.cm.dao.bean.ErrorCode;

/**
 * A data access object (DAO) providing persistence and search support for
 * ErrorCode entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.cwec.exchange.pojo.ErrorCode
 * @author MyEclipse Persistence Tools
 */
@Repository("ErrorCodeDAO")
public class ErrorCodeDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(ErrorCodeDAO.class);
	//定义全局变量genericFaultMap，用于存储系统中定义的所有异常代码
	private static Map<String, GenericFault> genericFaultMap = new HashMap<String, GenericFault>();
	// property constants
	public static final String ERROR_MESSAGE = "errorMessage";

	public ErrorCode save(ErrorCode transientInstance) {
		log.debug("saving ErrorCode instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
			return transientInstance;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ErrorCode persistentInstance) {
		log.debug("deleting ErrorCode instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * 用于获取系统中所有的异常代码
	 * 
	 * @param errorCode  异常代码编号
	 * 
	 * @return GenericFault  返回结果信息
	 */
	public GenericFault getGenericFault(String errorCode) {
		if (genericFaultMap.size() == 0) {
			synchronized (genericFaultMap) {
				List<ErrorCode> list = this.findAll();
				for (ErrorCode error : list) {
					GenericFault gf = new GenericFault(error.getErrorCode(),
							error.getErrorMessage());
					genericFaultMap.put(error.getErrorCode(), gf);
				}
			}
		}
		GenericFault gf = genericFaultMap.get(errorCode);
		if (gf == null) {
			gf = new GenericFault("99999", "interface exception");
		}
		return gf;

	}

	public ErrorCode findById(java.lang.String id) {
		log.debug("getting ErrorCode instance with id: " + id);
		try {
			ErrorCode instance = (ErrorCode) getHibernateTemplate().get(
					"com.cwec.exchange.pojo.ErrorCode", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<ErrorCode> findByExample(ErrorCode instance) {
		log.debug("finding ErrorCode instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ErrorCode instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ErrorCode as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<ErrorCode> findByErrorMessage(Object errorMessage) {
		return findByProperty(ERROR_MESSAGE, errorMessage);
	}

	public List findAll() {
		log.debug("finding all ErrorCode instances");
		try {
			String queryString = "from ErrorCode";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ErrorCode merge(ErrorCode detachedInstance) {
		log.debug("merging ErrorCode instance");
		try {
			ErrorCode result = (ErrorCode) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ErrorCode instance) {
		log.debug("attaching dirty ErrorCode instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ErrorCode instance) {
		log.debug("attaching clean ErrorCode instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}